home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Shared / MM / Scripts / NBInit.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  4.3 KB  |  136 lines

  1. //
  2. // Copyright 1999 Macromedia, Inc. All rights reserved.
  3. //
  4. //NBInit.js
  5. //
  6. // Adds or edits the MM_nbSetInitDown function, which gets attached
  7. // to the body onload handler
  8. //
  9. // Used by Set Nav Bar Image and Set Initially Down Images behaviors
  10. //
  11. //--------------------------------------------------------------
  12. //
  13. //
  14. // setInitDownFn()
  15. // removeInitDownArgs(imgName)
  16. // findArrInd(arr,item)
  17.  
  18.  
  19.  
  20. // function: setInitDownFn
  21. // description: add MM_nbInitDown function call if it doesn't exist
  22. // if it does exist, add arguments to end of function call
  23. // arguments: variable number of imageName and downSrc pairs, usually only 1
  24.  
  25. function setInitDownFn(){
  26.    var bodyNode = dreamweaver.getDocumentDOM('document').body;
  27.    var onLoadVal = bodyNode.onload;
  28.    var initDownFnExists = onLoadVal && onLoadVal.indexOf("MM_nbSetInitDown") != -1;
  29.    var setInitDownFnArgs = setInitDownFn.arguments;
  30.    var nArgs = setInitDownFnArgs.length;
  31.    var newFnStr = "MM_nbSetInitDown("
  32.    
  33.    // the nav bar group name is put in the function call so that it will be
  34.    // easy to build in user-defined groups at a later time.
  35.    // (right now, there can only be one nav bar group per page)
  36.    var navBarGroupName = "group1"; // default nav bar group name
  37.    
  38.    // determine if there is already a MM_nbSetInitDown fn call in onload handler
  39.    if ( initDownFnExists){
  40.       var currFnStr = getHandler(bodyNode,"onLoad","MM_nbSetInitDown");
  41.       var currFnArgs = extractArgs(currFnStr).slice(1);
  42.       
  43.       // add group name to argument call. We use what is already in the doc
  44.       // in case the user has changed it
  45.       newFnStr += quote(currFnArgs[0],1) + ",";
  46.       
  47.       // go through each name, downSrc pair. look for the name
  48.       // in the existing MM_nbInitDown function call. If the name isn't there,
  49.       // add the name and downSrc to the existing function.
  50.       // If the name is there and the downSrc matches what we are adding,
  51.       // don't add. If the 
  52.       // down src, then change the src.
  53.       for (i=0; i<nArgs; i+=2){
  54.          arrInd = findArrInd(currFnArgs,setInitDownFnArgs[i])
  55.          if ( arrInd == -1 ){  // image name is not in function call
  56.             newFnStr += quote(setInitDownFnArgs[i],1) + "," +
  57.                         quote(setInitDownFnArgs[i+1],1) + ",";
  58.          } else { 
  59.             if ( currFnArgs[arrInd+1] != setInitDownFnArgs[i+1] ){
  60.                currFnArgs[arrInd+1] = setInitDownFnArgs[i+1];
  61.             }
  62.          }
  63.       }
  64.       // combine old args and new args
  65.       // start at arg 1 because that is where the imgName,downSrc pairs start
  66.       // fn args are: groupName,imgName,downSrc...[imgName,downSrc]
  67.       for (i=1; i<currFnArgs.length; i++){
  68.          newFnStr += quote(currFnArgs[i],1) + ",";
  69.       }
  70.    } else { // create fn 
  71.       newFnStr += quote(navBarGroupName,1) + ",";
  72.       for (i=0; i<nArgs; i++){
  73.          newFnStr += quote(setInitDownFnArgs[i],1) + ",";
  74.       }
  75.    }
  76.    
  77.    newFnStr = newFnStr.substring(0,newFnStr.length-1) + ")";
  78.  
  79.    // re-attach function call
  80.    setHandler(bodyNode,"onload",newFnStr);
  81. }
  82.  
  83. // function: removeInitDownArgs(imgName)
  84. // description: removes image name and down source from MM_nbInitDown
  85. // function, if they exist. Deletes entire function if needed
  86.  
  87. function removeInitDownArgs(imgName){
  88.    var bodyNode = dreamweaver.getDocumentDOM('document').body;
  89.    var onLoadVal = bodyNode.onload;
  90.    var initDownFnExists = onLoadVal && onLoadVal.indexOf("MM_nbSetInitDown") != -1;
  91.    
  92.    if ( !initDownFnExists )
  93.       return;
  94.       
  95.    var initDownFnStr = getHandler(bodyNode,"onload","MM_nbSetInitDown");
  96.    var initDownArgs = extractArgs(initDownFnStr).slice(1);
  97.    var nArgs = initDownArgs.length;
  98.    var foundMatch = true;
  99.    
  100.    for (i=0; i<nArgs; i++){
  101.       if (initDownArgs[i] == imgName){
  102.          if (nArgs < 5)
  103.             removeHandler(bodyNode,"onload","MM_nbSetInitDown");
  104.          else {
  105.             initDownArgs = initDownArgs.slice(0,i).concat( initDownArgs.slice(i+2) )
  106.             foundMatch=true;
  107.          }
  108.          break;
  109.       }
  110.    }
  111.    if (foundMatch){
  112.       initDownFnStr = "MM_nbSetInitDown(" + initDownArgs.join(",") + ")";
  113.       setHandler(bodyNode,"onload",initDownFnStr);
  114.    }
  115. }
  116.  
  117. // function: findArrInd
  118. // description: returns integer determining if item is in the array
  119. // -1:  it is not included, a positive integer or 0 is the position of the
  120. // first match
  121.  
  122. function findArrInd(arr,item){
  123.    var nItems = arr.length;
  124.    var i;
  125.    var retVal = -1
  126.    
  127.    for (i=0; i<nItems; i++){
  128.       if (arr[i] == item){
  129.          retVal = i;
  130.          break;
  131.       }   
  132.    }
  133.    return retVal;
  134. }
  135.  
  136.